<?php

namespace app\admin\controller;

use app\common\controller\Backend;
use app\common\model\shop\MsgReminder;

/**
 * 客户资质审核
 *
 * @icon fa fa-circle-o
 */
class ArchivesAbility extends Backend
{

    /**
     * ArchivesAbility模型对象
     * @var \app\admin\model\ArchivesAbility
     */
    protected $model = null;

    public function _initialize()
    {
        parent::_initialize();
        $this->model = new \app\admin\model\ArchivesAbility;
        $this->view->assign("levelList", $this->model->getLevelList());
        $this->view->assign("examStatusList", $this->model->getExamStatusList());
    }

    /**
     * 默认生成的控制器所继承的父类中有index/add/edit/del/multi五个基础方法、destroy/restore/recyclebin三个回收站方法
     * 因此在当前控制器中可不用编写增删改查的代码,除非需要自己控制这部分逻辑
     * 需要将application/admin/library/traits/Backend.php中对应的方法复制到当前控制器,然后进行修改
     */

    /**
     * 查看
     */
    public function index()
    {
        //当前是否为关联查询
        $this->relationSearch = true;
        //设置过滤方法
        $this->request->filter(['strip_tags', 'trim']);
        if ($this->request->isAjax()) {
            //如果发送的来源是Selectpage，则转发到Selectpage
            if ($this->request->request('keyField')) {
                return $this->selectpage();
            }
            list($where, $sort, $order, $offset, $limit) = $this->buildparams();
            $total = $this->model
                ->with(['archives', 'admin'])
                ->where($where)
                ->order($sort, $order)
                ->count();

            $list = $this->model
                ->with(['archives', 'admin'])
                ->where($where)
                ->order($sort, $order)
                ->limit($offset, $limit)
                ->select();
            foreach ($list as $row) {

                $row->getRelation('archives')->visible(['name', 'mobile', 'level']);
                $row->getRelation('admin')->visible(['nickname']);
            }
            $list = collection($list)->toArray();
            $result = array("total" => $total, "rows" => $list);

            return json($result);
        }
        return $this->view->fetch();
    }

    /**
     * Notes:审核资质
     * DateTime 2021/6/9 10:04
     * @Author szh
     */
    public function exam()
    {
        $param = $this->request->param();
        $info = $this->model->get([
            'id' => $param['id']
        ]);
        if (!$info) {
            $this->error(__('No Results were found'));
        }
        if($info->exam_status == 2){
            return $this->error("资质已通过无法修改");
        }
        $info->exam_remark  = $nweLog['exam_remark'] = $exam_remark = $param['exam_remark'];
        $info->exam_status  = $nweLog['exam_status']  = $param['exam_status'];
        $info->exam_time    = $nweLog['exam_time']    = time();
        $info->exam_user_id = $nweLog['exam_user_id'] = $this->auth->id;
        $nweLog['licenseimage'] = $info->licenseimage;
        $nweLog['openimage'] = $info->openimage;
        $info->exam_log = json_encode(array_merge([$nweLog], json_decode($info->exam_log,true)?:[]));
        $result = $info->save();
        if($result !== false){
            //系统信息提示
            // type=10时候，min==2||min==3是资质上传审核信息通知
            // 约定 mid=3是不合格的通知 min=2是合格的通知
            $m_msg = new MsgReminder();
            $title = "资质上传审核";
            if($nweLog['exam_status'] == 3){
                $content = "本次审核结果为【不合格】，原因是：{$exam_remark}，请重新提交审核资质！";
                $mid = 3;
            }else{
                $content = "本次审核结果为【合格】";
                $mid = 2;
                //顺应磊磊的要求 如果通过就把没通过的消息提示删除
                $m_msg->where(['type' => 10, 'mid' => 3, 'to_user_id' => $info->shop_user_id])->delete();
            }
            $m_msg->addMsg(10, $title, $content, $mid, $info->shop_user_id);
            return $this->success('审核成功');
        }else{
            $this->error("审核失败");
        }
    }
}
